//
//  XYChatViewController.h
//  十年少
//
//  Created by 尧的mac on 15/11/28.
//  Copyright © 2015年 xxy.icom. All rights reserved.
//

#import "WLmessagesCell.h"
#import "WLMessagesFrame.h"

@interface WLmessagesCell ()

/**
 *  时间
 */
@property(nonatomic,weak)UILabel *timeView;
/**
 *  图像
 */
@property(nonatomic,weak)UIImageView *iconView;
/**
 *  内容
 */
@property(nonatomic,weak)UIButton *textView;
@end


@implementation WLmessagesCell
//自定义的cell初始化方法
+(instancetype)cellWithTableView:(UITableView *)tableView
{
//为了减少创建cell的数量定义一个标识
    static NSString *ID = @"mseeages";
    //在tableView缓存池中找这个标识，
    WLmessagesCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //如果为nil重新创建
    if (cell == nil) {
        cell = [[WLmessagesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
/**
 *  重写(初始化)自定义cell
 */
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //给cell添加控件(一次性的数据偶读添加到这里面)        
//        2.头像(UIImageView)
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
//        3.正文(因为正文可以点击复制粘贴所以用UIButton)
        UIButton *textView = [[UIButton alloc] init];
        textView.titleLabel.numberOfLines = 0; // 自动换行
//        textView.backgroundColor = [UIColor whiteColor];
        [textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        textView.titleLabel.font = [UIFont systemFontOfSize:15];
        //设置聊天内容的背景图片内边距
        textView.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        
        [self.contentView addSubview:textView];
        self.textView = textView;
 
        // 4.设置cell的背景色
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


//封装(主要封装的是修改后的图片尺寸，所以传过来的参数是图片，图片名称，状态，返回值是修改后的UIImage)
-(UIImage *)resizeImage:(NSString *)name
{
    UIImage *normal = [UIImage imageNamed:name];
    CGFloat w = normal.size.width * 0.5;
    CGFloat h = normal.size.height * 0.5;
    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
}

/**
 *  设置数据(重写Frame的setter方法)
 */
-(void)setMessagesFrame:(WLMessagesFrame *)messagesFrame
{
    _messagesFrame = messagesFrame;

    

//    2.图像
    NSString *icon = (self.messagesFrame.messages.type == WLMessagesTypeMe) ? @"me" : @"other";
    
    self.iconView.image = [UIImage imageNamed:icon];
    self.iconView.frame = self.messagesFrame.iconFrame;
    
//    3.内容
    [self.textView setTitle:self.messagesFrame.messages.text forState:UIControlStateNormal];
    self.textView.frame = self.messagesFrame.textFrame;

    //4.内容图片
    if (self.messagesFrame.messages.type == WLMessagesTypeMe) {
        
        //调用自定义的重构方法
        UIImage *lastImage = [self resizeImage:@"chat_send_nor.png"];
        
        [self.textView setBackgroundImage:lastImage forState:UIControlStateNormal];
        
        UIImage *lastImage1 = [self resizeImage:@"chat_send_press.png"];
        [self.textView setBackgroundImage:lastImage1 forState:UIControlStateHighlighted];
        
    }else{
        UIImage *lastImage = [self resizeImage:@"chat_recive_nor.png"];
        [self.textView setBackgroundImage:lastImage forState:UIControlStateNormal];
        
        UIImage *lastImage1 = [self resizeImage:@"chat_recive_press.png"];
        [self.textView setBackgroundImage:lastImage1 forState:UIControlStateHighlighted];
    }    
}

@end
